﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PalFramework.Object;

namespace PalBattleEmulator.Object
{
    public abstract class PalObject : IPalObject
    {
        protected static int type = 0;              //object类型
        protected static int length = 0;            //数组长度
        protected static bool[] mask;                //数组保存的mask
        protected static Dictionary<string, int> nameDictionary;        //属性名字典


        private int[] properties;

        public PalObject()
        {
            this.properties = new int[length];
        }


        /// <summary>
        /// 按Id获取或设置属性
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int this[int index]
        {
            get
            {
                if (index < length && index >= 0)
                {
                    return properties[index];
                }
                else
                {
                    Debug.Debug.Write("Invalid Property Index");
                    return 0;
                }
            }

            set
            {
                if (index < length && index >= 0)
                {
                    properties[index] = value;
                }
                else
                {
                    Debug.Debug.Write("Invalid Property Index");
                }
            }
        }

        /// <summary>
        /// 按名称获取或设置属性
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int this[string name]
        {
            get
            {
                if (nameDictionary.ContainsKey(name))
                {
                    return this[nameDictionary[name]];
                }
                else
                {
                    Debug.Debug.Write("Invalid Property Name");
                    return 0;
                }
            }
            set
            {
                if (nameDictionary.ContainsKey(name))
                {
                    this[nameDictionary[name]] = value;
                }
                else
                {
                    Debug.Debug.Write("Invalid Property Name");
                }
            }
        }

        public virtual byte[] Serialize()
        {
            if (mask != null)
            {
                using (BinaryWriter writer = new BinaryWriter(new MemoryStream()))
                {
                    for (int i = 0; i < length; i++)
                    {
                        if (mask[i])
                            writer.Write(properties[i]);
                    }
                    writer.Flush();
                    return ((MemoryStream)writer.BaseStream).GetBuffer();
                }
            }
            return null;
        }

        public virtual void Deserialize(byte[] buffer)
        {
            if (mask != null)
            {
                using (BinaryReader reader = new BinaryReader(new MemoryStream(buffer)))
                {
                    for (int i = 0; i < length; i++)
                    {
                        if (mask[i])
                            properties[i] = reader.ReadInt32();
                    }
                }
            }
        }
    }
}
